home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Frameworks / MacZoop 1.6.5 / Basic Classes / Z Sources / ZCommander.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-26  |  5.6 KB  |  229 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            ObjectMacZapp        -- a standard Mac OOP application template
  5. *
  6. *
  7. *
  8. *            ZCommander.cpp            -- an object for handling commands
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1996, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21. #include    "ZCommander.h"
  22. #include    "ZObjectArray.cpp"
  23. #include    "MacZoop.h"
  24.  
  25.  
  26. /*----------------------------------***  CONSTRUCTOR  ***----------------------------------*/
  27.  
  28. ZCommander::ZCommander( ZCommander* aBoss )
  29.     : ZComrade()
  30. {
  31.     itsBoss = aBoss;
  32.     itsUnderlings = NULL;
  33.     
  34.     if ( itsBoss )
  35.         itsBoss->AddUnderling( this );
  36. }
  37.  
  38. /*-----------------------------------***  DESTRUCTOR  ***----------------------------------*/
  39.  
  40. ZCommander::~ZCommander()
  41. {
  42.     // dispose of any objects whose boss this is 
  43.     
  44.     if ( itsUnderlings )
  45.         itsUnderlings->DisposeAll();
  46.     
  47.     // remove us from our boss
  48.  
  49.     if ( itsBoss )
  50.         itsBoss->RemoveUnderling( this );
  51. }
  52.  
  53.  
  54. /*-------------------------------***  HANDLECOMMAND  ***---------------------------------*/
  55. /*    
  56.  
  57. if it has a boss, it passes the command to it for handling
  58. ----------------------------------------------------------------------------------------*/
  59.  
  60. void    ZCommander::HandleCommand( const short menuID, const short itemID )
  61. {
  62.     if ( itsBoss )
  63.         itsBoss->HandleCommand( menuID, itemID );
  64. }
  65.  
  66.  
  67. /*-------------------------------***  HANDLECOMMAND  ***--------------------------------*/
  68. /*    
  69. pass command object to boss if there is one
  70. ----------------------------------------------------------------------------------------*/
  71.  
  72. void    ZCommander::HandleCommand( const long theCommand )
  73. {
  74.     // pass edit menu commands to the local handler methods. It is easier to override
  75.     // those methods than to override this method, but the choice is yours.
  76.     
  77.     switch ( theCommand )
  78.     {
  79.         case kCmdCut:
  80.             DoCut();
  81.             break;
  82.         case kCmdCopy:
  83.             DoCopy();
  84.             break;
  85.         case kCmdPaste:
  86.             DoPaste();
  87.             break;
  88.         case kCmdClear:
  89.             DoClear();
  90.             break;
  91.         case kCmdSelectAll:
  92.             DoSelectAll();
  93.             break;
  94.         default:
  95.             if ( itsBoss )
  96.                 itsBoss->HandleCommand( theCommand );
  97.             break;
  98.     }
  99. }
  100.  
  101.  
  102. /*-------------------------------***  UPDATEMENUS  ***---------------------------------*/
  103. /*    
  104.  
  105. if it has a boss, it asks it to update its menus
  106.  
  107. ----------------------------------------------------------------------------------------*/
  108.  
  109. void    ZCommander::UpdateMenus()
  110. {
  111.     // enable the paste command if there is something this object can
  112.     // paste.
  113.     
  114.     if ( CanPasteType())
  115.         gMenuBar->EnableCommand( kCmdPaste );
  116.     
  117.     if ( itsBoss )
  118.         itsBoss->UpdateMenus();
  119. }
  120.  
  121.  
  122.  
  123. /*-----------------------------***  HANDLEAPPLEEVENT  ***-------------------------------*/
  124. /*    
  125.  
  126. if it has a boss, it passes the command to it for handling. Override this to process
  127. apple events you are interested in, call the inherited method for others.
  128. ----------------------------------------------------------------------------------------*/
  129.  
  130. void    ZCommander::HandleAppleEvent(    AEEventClass aeClass, AEEventID aeID,
  131.                                         AppleEvent* aeEvt, AppleEvent* reply )
  132. {    
  133.     if ( itsBoss )
  134.         itsBoss->HandleAppleEvent( aeClass, aeID, aeEvt, reply );
  135.     else
  136.         FailOSErr( errAEEventNotHandled );
  137. }
  138.  
  139.  
  140. /*-----------------------------------***  IDLE  ***-------------------------------------*/
  141. /*    
  142.  
  143. called periodically if this is in the chain of command. It passes the call up to its boss.
  144.  
  145. ----------------------------------------------------------------------------------------*/
  146.  
  147. void    ZCommander::Idle()
  148. {
  149.     if ( itsBoss )
  150.         itsBoss->Idle();
  151. }
  152.  
  153.  
  154. /*-----------------------------------***  TYPE  ***-------------------------------------*/
  155. /*    
  156.  
  157. this user is typing while this commander is active
  158.  
  159. ----------------------------------------------------------------------------------------*/
  160.  
  161. void    ZCommander::Type( const char theKey )
  162. {
  163.     if ( itsBoss )
  164.         itsBoss->Type( theKey );
  165. }
  166.  
  167.  
  168. /*-------------------------------***  ADDUNDERLING  ***---------------------------------*/
  169. /*    
  170. adds <anUnderling> to its list of underlings
  171. ----------------------------------------------------------------------------------------*/
  172.  
  173. void    ZCommander::AddUnderling( ZCommander* anUnderling )
  174. {
  175.     if ( itsUnderlings == NULL )
  176.         FailNIL( itsUnderlings = new ZCommanderList());
  177.     
  178.     itsUnderlings->AppendItem( anUnderling );
  179. }
  180.  
  181. /*-----------------------------***  REMOVEUNDERLING  ***--------------------------------*/
  182. /*    
  183. removes <anUnderling> from its list of underlings
  184. ----------------------------------------------------------------------------------------*/
  185.  
  186. void    ZCommander::RemoveUnderling( ZCommander* anUnderling )
  187. {
  188.     if ( itsUnderlings && itsUnderlings->Contains( anUnderling ))
  189.     {
  190.         itsUnderlings->DeleteObject( anUnderling );
  191.         
  192.         if ( itsUnderlings->CountItems() < 1 )
  193.             ForgetObject( itsUnderlings );
  194.     }    
  195. }
  196.  
  197. /*--------------------------------***  SENDMESSAGE  ***---------------------------------*/
  198. /*    
  199. send the message to the boss as well as any nominated listeners
  200. ----------------------------------------------------------------------------------------*/
  201.  
  202. void    ZCommander::SendMessage( long aMessage, void* msgData )
  203. {
  204.     // we always send this message to our boss
  205.     
  206.     if ( itsBoss )
  207.         itsBoss->ReceiveMessage( this, aMessage, msgData );
  208.         
  209.     inherited::SendMessage( aMessage, msgData );
  210. }
  211.  
  212.  
  213. /*--------------------------------***  SENDMESSAGE  ***---------------------------------*/
  214. /*    
  215. send the message to the boss as well as any nominated listeners
  216. ----------------------------------------------------------------------------------------*/
  217.  
  218. void    ZCommander::SendMessage( ZMessage* aMessage )
  219. {
  220.     // we always send this message to our boss
  221.  
  222.     if ( itsBoss )
  223.         itsBoss->ReceiveMessage( this, aMessage );
  224.         
  225.     inherited::SendMessage( aMessage );
  226. }
  227.  
  228.  
  229.